﻿using CPPEI.Coolzon.Core;
using CPPEI.Coolzon.Core.Data;
using CPPEI.Coolzon.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CPPEI.Coolzon.ManagementCenter
{
    public partial class EditSolution : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            string Id = Request.QueryString["Id"].ToString();

            using (DbContext context = new DbContext())
            {
                ISolutionRepository solutionRepository = RepositoryFactory.Create<ISolutionRepository>();

                CPPEI.Coolzon.Core.Data.Solution solution = solutionRepository.GetSolution(context, new Guid(Id));
                hiddenid.Value = solution.Id.ToString();
                this.txtSolutionName.Text = solution.Name.ToString();
                this.txtSolutionCaption.Text = solution.Caption;
                this.ddlSolutionType.SelectedValue = solution.Type;
                this.chbIsEnable.Checked = solution.IsEnable;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.txtSolutionName.Text))
                {
                    lblHint.Text = "方案名不能为空";
                    return;
                }
                else if (string.IsNullOrWhiteSpace(this.txtSolutionCaption.Text))
                {
                    lblHint.Text = "方案说明不能为空";
                    return;
                }
                else if (string.IsNullOrWhiteSpace(this.ddlSolutionType.SelectedValue))
                {
                    lblHint.Text = "方案类型不能为空";
                    return;
                }

                Solution solution = new Solution();
                solution.Id = new Guid(hiddenid.Value);
                solution.Name = txtSolutionName.Text;
                solution.Caption = txtSolutionCaption.Text;
                solution.IsEnable = chbIsEnable.Checked;
                solution.Type = ddlSolutionType.SelectedValue;
                using (DbContext context = new DbContext())
                {
                    ISolutionRepository solutionRepository = RepositoryFactory.Create<ISolutionRepository>();
                    if (solutionRepository.UpdateSolution(context, solution))
                    {
                        lblHint.Text = "更新方案成功";
                        Response.Redirect("SolutionList.aspx", false);
                    }
                    else
                    {
                        lblHint.Text = "更新方案失败";
                    }
                }
            }
            catch (Exception ex)
            {
                lblHint.Text = "更新方案失败;" + ex.Message;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SolutionList.aspx", false);
        }
    }
}